package com.lmind.bean;

/**
 * 命名风格转换工具
 * @author 刘勉
 *
 */
public class NamingStyleUtil {
	
	/**
	 * 将 myName风格转换为my_name风格
	 * @param myName名称
	 * @return my_name风格
	 */
	public static String propertyToUnderscores(String name) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);
			if (i != 0 && Character.isUpperCase(c)) {
				sb.append("_" + Character.toLowerCase(c));
			} else {
				sb.append(Character.toLowerCase(c));
			}
		}
		return sb.toString();
	}
	
	
	/**
	 * 将 my_name风格转换为myName风格
	 * @param my_name名称
	 * @return myName风格
	 */
	public static String underscoresToProperty(String name) {
		StringBuilder sb = new StringBuilder();
		boolean wordBegin = false;
		for (int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);
			if (c == '_') {
				wordBegin = true;
			} else if (wordBegin) {
				sb.append(Character.toUpperCase(c));
				wordBegin = false;
			} else {
				sb.append(Character.toLowerCase(c));
			}
		}
		return sb.toString();
	}
}
